﻿#region Using directives
using System;
using System.ComponentModel;
using System.Linq;
using System.Web.Script.Services;
using System.Web.Services;

#endregion

namespace ProcolServices {
    /// <summary>
    /// Summary description for I18NService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    [ScriptService]
    public class I18NService : WebService {
        private readonly ProcolDataContext db = new ProcolDataContext();

        [WebMethod]
        public String GetTextAccordingToUserId(int uid, String key) {
            try {
                var query = (from i in db.I18NTexts
                             let user = (from u in db.Users
                                         where u.UserId == uid
                                         select u).Single()
                             where i.EntryId == key && i.LanguageId == user.LanguageId
                             select i).Single();

                return query.Text;
            } catch (InvalidOperationException) {
                return String.Format("NULL-{0}-{1}", uid, key);
            }
        }

        [WebMethod]
        public String GetTextAccordingToUser(User user, String key) {
            return GetTextAccordingToUserId(user.UserId, key);
        }

        [WebMethod]
        public String GetText(int language, String key) {
            try {
                var query = (from i in db.I18NTexts
                             where i.EntryId == key && i.LanguageId == language
                             select i).Single();

                return query.Text;
            } catch (InvalidOperationException) {
                return String.Format("NULL-L{0}-{1}", language, key);
            }
        }

        [WebMethod]
        public int AddNewI18NText(String entryId, int languageId, String text) {
            var values = new Object[] {entryId, languageId, text};
            foreach (var value in values) {
                String str = value.ToString();
                int tryParse;

                if (Int32.TryParse(str, out tryParse)) {
                    if (tryParse < 0)
                        return -2;
                } else if (String.IsNullOrEmpty(str))
                    return -2;
            }
            try {
                var newI18NText = new I18NText {EntryId = entryId, LanguageId = languageId, Text = text};
                db.I18NTexts.InsertOnSubmit(newI18NText);
                db.SubmitChanges();

                //**Same case as MajorService, see class for details 
                return newI18NText.LanguageId;
            } catch (InvalidOperationException) {
                return -2;
            }
        }

        [WebMethod]
        public I18NHolder[] GetAllI18N() {
            var query = from i in db.I18NTexts
                        orderby i.EntryId, i.LanguageId
                        select new I18NHolder { Id = i.I18NId, Language = i.LanguageId, EntryId = i.EntryId, Text = i.Text };

            return query.ToArray();
        }

        public class I18NHolder {
            public int Id { get; set; }
            public int Language { get; set; }
            public string EntryId { get; set; }
            public string Text { get; set; }
        }
    }
}